<?php
require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'autoload.php';

use payClass\Datebase;
use payClass\pay\Config;

/**
 * 异步通知
 */
class AsynNotify
{
    /**
     * 通知处理
     */
    public static function dealWith
    {
        $db        = new Datebase();

        $config    = new Config();

        $param     = $db->param();

        // file_put_contents('data.txt', json_encode($param)."\r\n",FILE_APPEND);

        $sign      = urldecode($param['sign']);

        $transdata = json_decode(urldecode($param['transdata']), true);
        // 生成签名
        $_sign     = Config::getSignMsg($transdata, Config::$key);

        if ($sign != $_sign) {

            exit('验签失败');

            // file_put_contents('data.txt', '验签失败'.$sign1."\r\n",FILE_APPEND);
        }

        $merchantTradeNo = $param['order_no'];

        $position        = strrpos($merchantTradeNo, 'U');

        $orderNumber     = substr($merchantTradeNo, 0, $position);

        $uid             = substr($merchantTradeNo, $position + 1);

        $res = $db->runUpdate([
            'money'         =>  $transdata['order_amount'],
            'order_number'  =>  $transdata['order_amount'],
            'uid'           =>  $uid,
        ]);

        if (!$res) {

            exit('上分失败');

            // file_put_contents('data.txt', '上分失败'.$sign1."\r\n",FILE_APPEND);
        }

        exit('Response status code 200');
    }
}

AsynNotify::dealWith();
